﻿var token = localStorage.getItem("token");
$(document).ready(function () {
    ShowTime();
});
// 表单提交事件
var Datetimenow = new Date();
var year = Datetimenow.getFullYear() + '-' + (Datetimenow.getMonth() + 1) + '-' + Datetimenow.getDate() + ' ' + Datetimenow.getHours() + ':' + Datetimenow.getMinutes() + ':' + Datetimenow.getSeconds();
function ShowTime() {
    var ids = [];
    var cheddata = [];

    layui.use('table', function () {
        var table = layui.table;
        var layer = layui.layer;
        // 新库位
        let movelocationlist = {
            3: '默认报损库位',
            4: '报损库位',
            5: '报损库位',
            6: '报损库位',
            7: '报损仓库',
            8: '报损库位',
            9: '默认库位',
            11: '默认新增库位',
            24: '新品仓库',
            41: '废品库位'
        };
        //渲染表格的函数
        function renderTable(pageNum) {
            //获取查询条件
            var ProductsCode = $("#ProductsCode").val();
            var ProductsName = $("#ProductsName").val();
            var WarehouseId = $("#WarehouseId").val();

            let showWarehouseledger = {};
            showWarehouseledger.ProductsCode = ProductsCode;
            showWarehouseledger.ProductsName = ProductsName;
            showWarehouseledger.WarehouseId = WarehouseId;
            showWarehouseledger.PageIndex = 1;
            showWarehouseledger.PageSize = 1000;
            showWarehouseledger.Token = token;
            //发送AJAX请求
            $.ajax({
                url: '/Warehouseledger/GetWarehouseledgerShow',
                type: 'get',
                data: showWarehouseledger,
                dataType: 'json',
                success: function (res) {
                    var data = JSON.parse(res)
                    console.log(data)
                    //渲染表格
                    table.render({
                        elem: '#dataTable',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'productsCode', title: '产品编号' },
                            { field: 'productsName', title: '产品名称' },
                            { field: 'specs', title: '产品规格' },
                            { field: 'warehouseName', title: '仓库' },
                            { field: 'procurementNum', title: '数量' },
                            {
                                field: 'storehouseId', title: '原库位', templet: function (d) {
                                    return movelocationlist[d.storehouseId];
                                }
                            },
                            {
                                field: 'addTime', title: '日期', templet: function (d) {
                                    let dateobj = new Date(d.addTime);
                                    let dateyear = dateobj.getFullYear();
                                    let datemonth = ('0' + (dateobj.getMonth() + 1)).slice(-2);
                                    let dateday = ('0' + dateobj.getDate()).slice(-2);
                                    let datehours = ('0' + (dateobj.getHours())).slice(-2);
                                    let dateminutes = ('0' + (dateobj.getMinutes())).slice(-2);
                                    let dateseconds = ('0' + (dateobj.getSeconds())).slice(-2);
                                    return dateyear + '-' + datemonth + '-' + dateday + ' ' + datehours + ':' + dateminutes + ':' + dateseconds;
                                }
                            },
                        ]],
                        toolbar: "#toolbarDemo",
                        defaultToolbar: ["exports"],
                        data: data.data.list,  // 数据源
                        page: true,   // 启用分
                        limit: 10,    // 每页显示多少条数据
                        limits: [10, 20, 30],  // 分页选择项
                        curr: pageNum,  // 当前页
                        total: data.data.listcount,  // 总记录数
                        loading: true // 启用加载状态
                    });
                },
                Error: function (xhr, status, error) {
                    console.log("请求失败" + error);
                }
            });
        }
        // 初次加载表格数据
        renderTable(1);
        // 查询按钮点击事件
        $('#searchBtnClient').click(function () {
            renderTable(1);  // 重置为第一页，并重新加载数据
        });
    })
}

